package services;

import dao.*;
import models.*;
import play.db.jpa.JPAApi;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import java.util.stream.Stream;

import static java.util.concurrent.CompletableFuture.supplyAsync;


/**
 * Provide JPA operations running inside of a thread pool sized to the connection pool
 */
public class WeigherInfo_Repository_JPA implements WeigherInfo_Repository {

    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public WeigherInfo_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }

    @SuppressWarnings("unchecked")
    @Override
    public CompletionStage<List<WeigherInfo_F_Entity>> getWeigher(String PlaceName, String TranType, String ScaleNo) {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
                // 插入一个原生SQL的测试,调用了SQLServer的自定义函数.
                String strSql = "select * from dbo.f_LoadDeliveryPlace('" + PlaceName+ "','"+ TranType +"','"+ ScaleNo +"')";
                return entityManager.createNativeQuery(strSql, WeigherInfo_F_Entity.class).getResultList();
            });
        }, executionContext);
    }
}
